package cn.wolfcode.luowowo.mgrsite.web.controller;

import cn.wolfcode.luowowo.article.domain.Strategy;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTheme;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.domain.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.cglib.reflect.MethodDelegate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategyDetail")
public class StrategyDetailController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyService strategyService;

    @Reference
    private IStrategyThemeService strategyThemeService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private  IStrategyTagService strategyTagService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")StrategyDetailQuery qo){
        //pageInfo
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategyDetail/list";
    }

    /**
     * 编辑页面
     * @param id
     * @return
     */
    @RequestMapping("/input")
    public String input(Long id, Model model){

        List<Strategy> strategies  = strategyService.list();

        //获取所有的主题
        List<StrategyTheme> themes = strategyThemeService.list();

        StrategyDetail strategyDetail = strategyDetailService.get(id);

        model.addAttribute("strategies",strategies);
        model.addAttribute("themes",themes);
        model.addAttribute("strategyDetail",strategyDetail);

        String tags = strategyTagService.getTagsByDetailId(id);
        model.addAttribute("tags",tags);

        return "strategyDetail/input";
    }

    /**
     * 二级联动获取攻略下的分类
     * @param strategyId
     * @return
     */
    @RequestMapping("/getCatalogByStrategyId")
    @ResponseBody
    public Object getCatalogByStrategyId(Long strategyId){

        List<StrategyCatalog> strategyCatalog  = strategyCatalogService.getCatalogByStrategyId(strategyId);

        return strategyCatalog;
    }

    /**
     * 攻略内容编辑
     * @param strategyDetail
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(StrategyDetail strategyDetail,String tags){
        AjaxResult ajaxResult = new AjaxResult();

        strategyDetailService.saveOrUpdate(strategyDetail,tags);

        return ajaxResult;
    }

    /**
     * 攻略内容删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        AjaxResult ajaxResult = new AjaxResult();

        strategyDetailService.delete(id);

        return ajaxResult;
    }

    /**
     * 上架下架
     * @param state
     * @param id
     * @return
     */
    @RequestMapping("/changeState")
    @ResponseBody
    public AjaxResult changeState(Integer state,Long id){
        AjaxResult ajaxResult = new AjaxResult();

        strategyDetailService.changeState(state,id);

        return ajaxResult;
    }




}
